from django.db import models


# 设计用户模型

class User(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=120)
    logo = models.ImageField(upload_to="logo")
    level = models.SmallIntegerField(default=1)
    phone = models.CharField(max_length=11, verbose_name="绑定手机", null=True)
    role = models.ForeignKey("Role", on_delete=models.SET_NULL, null=True)


# 角色表
class Role(models.Model):
    name = models.CharField(max_length=20, verbose_name="角色名")
    desc = models.CharField(max_length=50, verbose_name="角色描述")
    status = models.SmallIntegerField(default=1)  # 1表示 正常  2表示 不可以

    class Meta:
        db_table = "roles"


# 菜单的权限表
class ControlMenu(models.Model):
    name = models.CharField(max_length=20, verbose_name="名称")
    path = models.CharField(max_length=30, verbose_name="路由", null=True)
    icon = models.CharField(max_length=30, verbose_name="图标", null=True)
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True)


# 接口权限表
class Control(models.Model):
    name = models.CharField(max_length=20)
    path = models.CharField(max_length=20, null=True)
    method = models.CharField(max_length=10, null=True)

    class Meta:
        db_table = "controls"


# 角色接口关系表
class RoleControl(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    control = models.ForeignKey(Control, on_delete=models.CASCADE)

    class Meta:
        db_table = "role_control"


# 角色菜单关系表

class RoleControlMenu(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    control = models.ForeignKey(ControlMenu, on_delete=models.CASCADE)
